package com.csii.itest;

import java.awt.Color;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.FileDataSource;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class ItextPDF {

	public void CreatePDF(String strFileName){
		File file = new File(strFileName);
		Document doc = new Document(PageSize.A4,50,50,50,50);
		try {
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(file));
			//���ü��ܵ��û�����,����������,�ɸ���,�ɴ�ӡ,����λ��
			//writer.setEncryption("1".getBytes(), "2".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); 
		    writer.setEncryption(null, "2".getBytes(), PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); 
		    //���ĵ�
		    writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
		    doc.open();
		    BaseColor color = new BaseColor(new Color(0, 0, 255));
		    //��������Ĵ�С,��ɫ,��ʽ,FontFactory����ָ�����������.
		    Font font = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, color);
		    Font font1 = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, color);
		    //���ö�����Ⲣ��������
		    Paragraph chapterTitle = new Paragraph("Chapter 1",font);
		    //������һ���½ڵĶ��󣬱���Ϊ"Chapter 1"
		    Chapter chapter1 = new Chapter(chapterTitle,1);
		    //����Ϊ0,���ڱ���ǰ��û�б��.��֮...
		    chapter1.setNumberDepth(0);
		    //�����½��µ����½�.
		    Paragraph sectionTitle = new Paragraph("Section 1 of Chapter 1", font1);
		    //��Ӷ���,����chapter1��
		    Section section1 = chapter1.addSection(sectionTitle);

		    String bs = "";
		    InputStream inputStream = new FileInputStream("D:/datasource.doc");
		    DataInputStream stream = new DataInputStream(inputStream);
		    try {
		    	//stream.read(byte[] b,0,stream.available()){
		    		
		    	//}
				bs = stream.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    //д�ı�����
		    Paragraph text = new Paragraph(MyPhrase(bs));
		    section1.add(text);
		    text = new Paragraph(MyPhrase("�ĵ�����"));
		    section1.add(text);
		    doc.add(chapter1);

		    doc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * ���������ֵ��ַ���Ϣ
	 * @param strText
	 * @return
	 */
	public Phrase MyPhrase(String strText){
		//������������
	    BaseFont bfSong = null;
	    Font fontSong = null;
	    Phrase phrase = null;
		try {
			bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",false);
			fontSong = new Font(bfSong, 10, Font.NORMAL);
			phrase = new Phrase(strText,fontSong);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return phrase;
	}
	
	public static void main(String[] args) {
		   ItextPDF pdf = new ItextPDF();
		    String fileName = "D:\\tempPDF.pdf";
		    pdf.CreatePDF(fileName);
	 }

}
